#include "DeductionView.h"

#include <boost/bind.hpp>
#include "../../../utils/LateEvents.hpp"

using namespace Views::Graphic;
using namespace Views::Helpers::Graphic::Deduction;
using namespace Utils;

DeductionView::DeductionView(GUI::Form* form)
	: BaseGraphicView(form), searched_cards_dialog(NULL), deducted_card_pick_dialog(NULL), deducted_cards_dialog(NULL)
{
}

void DeductionView::openPickSearchedCardsDialog(Players::Player* player, Cards::PlayersCards players_cards,
	Cards::WeaponsCards weapons_cards)
{
	searched_cards_dialog = new SearchedCardsDialog(players_cards, weapons_cards);
	Form->addChild(searched_cards_dialog);

	searched_cards_dialog->doOnCardsPick(LateAppLoopEventFactory::create(&DeductionView::onSearchedCardsPick, this));
	searched_cards_dialog->MoveCenter();

	Form->Draw(true);
}

void DeductionView::openPickCardDialog(Players::Player* player, Cards::CardSet& common_cards)
{
	Cards::PlayerCard* player_card = NULL;
	Cards::WeaponCard* weapon_card = NULL;
	Cards::RoomCard* room_card = NULL;
	for(Cards::CardSet::Iterator i = common_cards.begin(); i != common_cards.end(); ++i)
	{
		if (dynamic_cast<Cards::PlayerCard*>(*i) != NULL)
		{
			player_card = dynamic_cast<Cards::PlayerCard*>(*i);
		}
		else if (dynamic_cast<Cards::WeaponCard*>(*i) != NULL)
		{
			weapon_card = dynamic_cast<Cards::WeaponCard*>(*i);
		}
		else if (dynamic_cast<Cards::RoomCard*>(*i) != NULL)
		{
			room_card = dynamic_cast<Cards::RoomCard*>(*i);
		}
	}
	deducted_card_pick_dialog = new DeductedCardPickDialog(player_card, weapon_card, room_card);
	Form->addChild(deducted_card_pick_dialog);

	deducted_card_pick_dialog->MoveCenter();
	deducted_card_pick_dialog->doOnCardPick(LateAppLoopEventFactory::create(&DeductionView::onDeductedCardPick, this));

	Form->Draw(true);
}

void DeductionView::onDeductedCardPick(Cards::Card* card)
{
	delete deducted_card_pick_dialog;
	Form->Draw(true);
	deductedCardPickEvent(card);
}

void DeductionView::onSearchedCardsPick(Cards::PlayerCard* player, Cards::WeaponCard* weapon)
{
	delete searched_cards_dialog;
	searched_cards_dialog = NULL;
	Form->Draw(true);
	Form->Display();
	searchedCardsPickEvent(player, weapon);
}

void DeductionView::showDeductedCard(Cards::Card* card, Players::Player* current_player, Players::Player* card_owner)
{
	deducted_cards_dialog = new DeductedCardsDialog();
	deducted_cards_dialog->doOnClose(LateAppLoopEventFactory::create(&DeductionView::onDeductedCardsDialogClose, this));
	deducted_cards_dialog->showDeductedCard(card, card_owner);
	deducted_cards_dialog->Move(300, 200);

	Form->addChild(deducted_cards_dialog);
	Form->Draw(true);
	Form->Display();
}

void DeductionView::showNoCardsDeducted(Cards::CardSet& cards, Players::Player* current_player)
{
	deducted_cards_dialog = new DeductedCardsDialog();
	deducted_cards_dialog->doOnClose(LateAppLoopEventFactory::create(&DeductionView::onDeductedCardsDialogClose, this));
	deducted_cards_dialog->showNoCardsDeducted(cards);
	deducted_cards_dialog->Move(300, 200);

	Form->addChild(deducted_cards_dialog);
	Form->Draw(true);
	Form->Display();
}

void DeductionView::onDeductedCardsDialogClose()
{
	delete deducted_cards_dialog;
	deducted_cards_dialog = NULL;
	deductedCardsShowEvent();
}

